package com.zstu.mall.web.admin;

import com.zstu.mall.entity.Product;
import com.zstu.mall.entity.ProductClass;
import com.zstu.mall.entity.pojo.ResultBean;
import com.zstu.mall.service.ProductClassService;
import com.zstu.mall.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin/classification")
public class AdminClassificationController {

    @Autowired
    private ProductClassService productClassService;

    @Autowired
    private ProductService productService;

    /**
     * 返回列表页面
     *
     * @param
     * @return
     */
    @RequestMapping("/toList.html")
    public String toList() {
        return "admin/category/list";
    }

    /**
     * 打开添加分类页面
     *
     * @param type
     * @return
     */
    @RequestMapping("/toAdd.html")
    public String toAdd(String type) {
        return "admin/category/add";
    }

    /**
     * 打开编辑页面
     *
     * @param id
     * @param type
     * @param map
     * @return
     */
    @RequestMapping("/toEdit.html")
    public String toEdit(String id, String type, Map<String, Object> map) {
        ProductClass productClass = productClassService.get(id);
        map.put("cate", productClass);
        return "admin/category/edit";
    }

    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/add.do")
    public ResultBean<Boolean> add(String id,  String type) {
        ProductClass productClass = new ProductClass();
        productClass.setId(id);
        productClass.setType(type);
        productClassService.create(productClass);
        return new ResultBean<>(true);
    }

    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/update.do")
    public ResultBean<Boolean> update(String id, String status,  String type) {
        ProductClass productClass = productClassService.get(id);
        productClass.setStatus(status);
        productClass.setType(type);
        productClassService.update(productClass);
        return new ResultBean<>(true);
    }

    @ResponseBody
    @RequestMapping("/del.do")
    public ResultBean<Boolean> del(String id) {
        productClassService.delete(id);
        return new ResultBean<>(true);
    }

    @RequestMapping("/list.do")
    @ResponseBody
    public ResultBean<List<Product>> findAll(String type,
                                             int pageindex, @RequestParam(value = "pageSize", defaultValue = "15") int pageSize) {
        List<Product> list = new ArrayList<>();
        if (pageindex == -1)
            list = productService.findByType(type, null);
        else {
            Pageable pageable = PageRequest.of(pageindex, pageSize);
            list = productService.findByType(type, pageable);
        }
        return new ResultBean<>(list);
    }

    /*@ResponseBody
    @RequestMapping("/getTotal.do")
    public ResultBean<Integer> getTotal(String type) {
        Pageable pageable = new PageRequest(1, 30, null);
        int count = (int) productService.findByType(type, pageable).getTotalElements();
        return new ResultBean<>(count);
    }*/
}